package com.group.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.pojo.Friends;
import com.group.pojo.UserAddress;
import com.group.service.FriendsService;
import com.group.service.UserAddressService;
import com.group.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author group
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/group/useraddres")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @ApiOperation(value = "按条件或全部查询信息")
    @RequestMapping("/findAddress")
    public R findAddress(Integer ids,Integer pageNo,Integer pageSize){
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }
        System.out.println(ids);
        PageHelper.startPage(pageNo,pageSize);
        /*if (ids==null){
            List<UserAddress> list = userAddressService.findAddress(null);
            PageInfo<UserAddress> pageInfo = new PageInfo<>(list);
            return R.ok().data("pageInfo",pageInfo);
        }else {
            UserAddress userAddress = new UserAddress();
            String idstr[] = ids.split(",");
            if(!idstr[0].equals("-1")) {userAddress.setAddressid( Integer.parseInt(idstr[0])  );}
            if(!idstr[1].equals("-1")) {userAddress.setUserid( Integer.parseInt(idstr[1])  );}
            if(!idstr[2].equals("-1")) {userAddress.setUsernameaddress( idstr[2] );}
            if(!idstr[3].equals("-1")) {userAddress.setPhone( idstr[3] );}
            if(!idstr[4].equals("-1")) {userAddress.setCampus( Integer.parseInt(idstr[4])  );}
            if(!idstr[5].equals("-1")) {userAddress.setRegion( Integer.parseInt(idstr[5])  );}*/

        List<UserAddress> list = userAddressService.findAddress(ids);
        PageInfo<UserAddress> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageInfo",pageInfo);
        /*}*/

    }

    @ApiOperation(value = "修改信息")
    @RequestMapping("/updateFriendsAddress1")
    public R updateFriendsAddress1(@RequestBody UserAddress userAddress){

        System.out.println(userAddress);
        boolean flag = userAddressService.updateFriendsAddress1(userAddress);
        return flag? R.ok() : R.error();
    }

    @ApiOperation(value = "标记删除")
    @RequestMapping("/deleteFriendsAddress1")
    public R deleteFriendsAddress1(String ids){
        System.out.println(ids);
        String idstr[] = ids.split(",");
        boolean flag = userAddressService.deleteFriendsAddress1(idstr);
        return  flag? R.ok() : R.error();
    }


    @ApiOperation(value = "默认地址")
    @GetMapping("/defaultAddress1")
    public R defaultAddress1(Integer addressid){
        System.out.println("id="+addressid);
        UserAddress userAddress = new UserAddress();
        userAddress.setAddressdefault(1);
        List<UserAddress> list = userAddressService.findAddress(addressid);
        for(int i=0;i<list.size();i++){
            userAddressService.defaultAddress01(list.get(i).getAddressid());
        }

        boolean flag = userAddressService.defaultAddress11(addressid);
        return  flag? R.ok() : R.error();
    }

    @ApiOperation(value = "添加功能")
    @PostMapping("/addFriendsAddress1")
    public R addFriendsAddress1(@RequestBody UserAddress userAddress){
        System.out.println("22222222222222");
        if(userAddress!=null){
            System.out.println("11111111111");
        }
        boolean flag = userAddressService.addFriendsAddress1(userAddress);
        return  flag? R.ok() : R.error();
    }

    @ApiOperation(value = "按地址查询id")
    @RequestMapping("/findIdByAddress")
    public Integer findIdByAddress(String address){
        Integer id=userAddressService.findIdByAddress(address);
        return id;
    }
    @ApiOperation(value = "根据id查地址")
    @RequestMapping("/findAddressById")
    public String findAddressById(Integer id){
        String address=userAddressService.findAddressById(id);
        return address;
    }


}

